<?php


namespace app\controller\admin;


use app\model\user\UserInvoiceModel;
use app\model\user\UserMessageModel;
use app\model\user\UserModel;
use think\App;
use think\facade\Db;

class PayController extends BaseAdminController
{


    function setModel()
    {
        return new UserInvoiceModel();
    }

    function setWithModel()
    {
        return ['user', 'auditor1Name', 'auditor2Name'];
    }

    function setSearch()
    {
        // TODO: Implement setSearch() method.
    }

    function setSort()
    {
        // TODO: Implement setSort() method.
    }

    /**
     * 审核员列表
     * @return \think\response\Json
     */
    public function getAuditorList()
    {
        try {
            $first = UserModel::where('status', 1)->where('type', 2)->field('username,id')->select();
            $two = UserModel::where('status', 1)->where('type', 3)->field('username,id')->select();
            return $this->success(['first' => $first, 'two' => $two]);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 指派审核员
     * @return \think\response\Json
     */
    public function assignAuditor()
    {
        try {
            $orderId = $this->request->param('order_id', 0, 'int');
            $auditor1 = $this->request->param('auditor1', 0, 'int');
            $auditor2 = $this->request->param('auditor2', 0, 'int');

            $userInvoiceModel = UserInvoiceModel::find($orderId);
            if (!$userInvoiceModel) {
                throw new \Exception('信息不存在');
            }
            $userInvoiceModel->auditor1 = $auditor1;
            $userInvoiceModel->auditor2 = $auditor2;
            $userInvoiceModel->assign_time = time();
            $userInvoiceModel->save();

            $userMessageModel = new UserMessageModel();
            $userMessageModel->saveAll([
                [
                    'user_id' => $auditor1,
                    'type' => 0
                ],
                [
                    'user_id' => $auditor2,
                    'type' => 0
                ],
            ]);
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 审核
     * @return \think\response\Json
     */
    public function confirm()
    {
        Db::startTrans();
        try {
            $ids = $this->request->param('id');
            // 2审核通过 3审核拒绝
            $status = $this->request->param('status');
            if (!in_array($status, [2, 3])) {
                throw new \Exception('状态错误');
            }
            if (is_array($ids)) {
                foreach ($ids as $id) {
                    $info = UserInvoiceModel::find($id);
                    if (!$info) {
                        throw new \Exception('信息不存在');
                    }
                    if ($info['status'] != 1) {
                        throw new \Exception('状态错误');
                    }
                    $info->status = $status;
                    $info->audit1_time = time();
                    $info->audit1_status = $status == 2 ? 1 : 2;
                    $info->audit2_time = time();
                    $info->audit2_status = $status == 2 ? 1 : 2;
                    $info->save();
                }
            } else {
                $info = UserInvoiceModel::find($ids);
                if (!$info) {
                    throw new \Exception('信息不存在');
                }
                if ($info['status'] != 1) {
                    throw new \Exception('状态错误');
                }
                $info->status = $status;
                $info->audit1_time = time();
                $info->audit1_status = $status == 2 ? 1 : 2;
                $info->audit2_time = time();
                $info->audit2_status = $status == 2 ? 1 : 2;
                $info->save();
            }

            Db::commit();
            return $this->success();
        } catch (\Exception $exception) {
            Db::rollback();
            return $this->error($exception);
        }
    }
}